<template>
  <a-modal
    :title="`子公司${model&&model.id>0?'编辑':'新增'}`"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" :labelCol="{ sm: { span: 4 } }" :wrapperCol="{ sm: { span: 20 } }" :selfUpdate="true">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="子公司名称">
          <a-input v-decorator="['name', { rules: [{ required: true, message: '子公司名称不能为空' }]}]" placeholder="请输入子公司名称"/>
        </a-form-item>
        <a-form-item label="地区">
          <a-cascader 
            v-decorator="['address']"
            :options="cityArr" 
            placeholder="地区"  
            :fieldNames="{label:'name',children:'children',value:'name'}"/>
        </a-form-item>
        <a-form-item label="团队人数" >
          <a-input-number :min="0" v-decorator="['teamUser']" />
        </a-form-item>
        <a-form-item label="描述">
          <a-textarea v-decorator="['remark']" :rows="4" placeholder="请输入子公司描述" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import cloneDeep from 'lodash.clonedeep'
import { cityArr } from '@/utils/ccity'
import { Cascader } from 'ant-design-vue'
// 表单字段
const fields = [ 'name' , 'teamUser', 'id', 'remark']
// 'remark'

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    country: {
      type: Array,
      default: () => [],
    },
    provinceArr: {
      type: Array,
      default: () => [],
    },
    levelArr: {
      type: Array,
      default: () => [],
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      city: '',
      cityArr
    }
  },
  components:{
    'a-cascader': Cascader,
  },
  created() {
    

    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
      if(this.model && this.model.city&&this.model.country&&this.model.province){
        setTimeout(()=>{
          this.model && this.form.setFieldsValue({'address':[this.model.country, this.model.province, this.model.city]})
        })
      }
    })
  },
  methods: {
    handleChange(value) {
      this.city = value
      this.form.setFieldsValue({city:''})
    },
  },
  computed: {
    c_cityArr: function () {
      console.log(this.form)
      let provinceArr = cloneDeep(this.provinceArr)
      let queryParam = cloneDeep(this.city)
      console.log(provinceArr, queryParam)

      if (queryParam) {
        let i = provinceArr.findIndex((v) => v.name == queryParam)
        console.log(provinceArr[i])
        return provinceArr[i].cityList
      } else {
        return []
      }
    },
  },
}
</script>
